package controller;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;

import model.User;
import view.ApplicationView;
import business.exceptions.BusinessServiceException;
import business.service.UserBusinessService;

//action listenerek
public class ApplicationController {

	private UserBusinessService ubs;

	public ApplicationController(UserBusinessService ubs) {
		this.ubs = ubs;
		ApplicationView ap = new ApplicationView(ubs, this);
		ap.setSize(new Dimension(500, 500));
		ap.setVisible(true);

	}

	public ActionListener getNewButtonActionListener(final ApplicationView aw) {

		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				aw.resetData();
				aw.setPersonId(0);
				aw.setStatus("");
			}
		};
	};

	public ActionListener getDeleteButtonActionListener(final ApplicationView aw) {
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int personid = aw.getPersonId();
				User p;
				try {
					if (personid == ubs.getMinMaxId("max")) {
						p = ubs.getPerson(aw.getPersonId(), "<");
						aw.loadData(p);
					} else if (personid == ubs.getMinMaxId("min")) {
						p = ubs.getPerson(aw.getPersonId(), ">");
						aw.loadData(p);
					} else {
						p = ubs.getPerson(aw.getPersonId(), ">");
						aw.loadData(p);
					}

					ubs.deletePerson(personid);
					JOptionPane.showMessageDialog(aw,
							"Utilizator sters cu succes!");
					aw.setPersonId(p.getIdperson());
				} catch (BusinessServiceException e) {
					JOptionPane.showMessageDialog(aw, e);
				}

				aw.setButtons();
			}
		};
	};

	public ActionListener getPrevActionListener(final ApplicationView aw) {

		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					aw.loadData(ubs.getPerson(aw.getPersonId(), "<"));
				} catch (BusinessServiceException e) {
					JOptionPane.showMessageDialog(aw, e);
				}
				aw.setDelete(true);

			}
		};
	};

	public ActionListener getSaveActionListener(final ApplicationView aw) {

		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// new user
				System.out.println("PersonId=" + aw.getPersonId());
				if (aw.getPersonId() == 0) {
					User p = aw.getNewPerson();
					if (p == null)
						return;
					try {
						int id = ubs.insertPerson(aw.getNewPerson());
						aw.setDelete(true);
						aw.setPersonId(id);
						aw.loadData(id);
					} catch (BusinessServiceException ex) {
						JOptionPane.showMessageDialog(aw, ex);
					}
				} else {
					User pnew = aw.getNewPerson();
					if (pnew != null) {
						try {
							ubs.updatePerson(pnew);
						} catch (BusinessServiceException ex) {
							JOptionPane.showMessageDialog(aw, ex);
						}

					}
				}
				aw.setCancel(false);
			}
		};
	};

	public ActionListener getNextActionListener(final ApplicationView aw) {

		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					aw.loadData(ubs.getPerson(aw.getPersonId(), ">"));
					aw.setDelete(true);
				} catch (BusinessServiceException ex) {

					JOptionPane.showMessageDialog(aw, ex);
				}
			}
		};
	};

	public ActionListener getCancelButtonActionListener(final ApplicationView aw) {

		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				aw.loadData(0);
				aw.setCancel(false);
			}
		};
	};

	public ActionListener getSearchButtonActionListener(final ApplicationView aw) {

		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				aw.openSearch(ApplicationController.this);
			}
		};
	};

	public ActionListener getSearchActionListener(final ApplicationView aw) {

		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
				User myPersons[];
				myPersons = ubs.searchUser(aw.getSearchText());
				aw.showSearchResults(myPersons, ApplicationController.this);
				} catch (BusinessServiceException ex) {
					JOptionPane.showMessageDialog(aw, ex);
				}

			}
		};
	};

	public MouseAdapter getSearchResults(final ApplicationView aw) {
		return new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					aw.getClickedPerson();
				}
			}
		};
	};
}
